<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

	private $layout = '';
	private $title = '';
	private $meta_http_equiv = array();
	private $meta_name = array();
	private $javascript_variables = array();
	private $javascripts = array();
	private $stylesheets = array();
	protected $global_data = array();

	public function __construct() {
		parent::__construct();
		$this->load->library('session');
	}

	protected function set_layout($layout) {
		if (!empty($layout)) {
			$this->layout = $layout;
		}
	}

	protected function set_title($title) {
		if (!empty($title)) {
			$this->title = $title;
		}
	}

	protected function set_meta($type, $key, $value) {
		if ($type == 'name') {
			$this->meta_name[$key] = $value;
		} else if ($type == 'http-equiv') {
			$this->meta_http_equiv[$key] = $value;
		}
	}

	protected function set_js($key, $value) {
		$this->javascript_variables[$key] = $value;
	}

	protected function load_js($javascripts) {
		if (!empty($javascripts)) {
			if (is_array($javascripts)) {
				$this->javascripts = array_merge($this->javascripts, $javascripts);
			} else {
				array_push($this->javascripts, $javascripts);
			}
		}
	}

	protected function load_css($stylesheets) {
		if (!empty($stylesheets)) {
			if (is_array($stylesheets)) {
				$this->stylesheets = array_merge($this->stylesheets, $stylesheets);
			} else {
				array_push($this->stylesheets, $stylesheets);
			}
		}
	}

	protected function set_data($data = array()) {
		$this->global_data = array_merge($this->global_data, $data);
	}

	protected function rend($template, $data = array()) {
		$data['l_javascripts'] = array_unique($this->javascripts);
		$data['l_stylesheets'] = $this->stylesheets;
		$data['l_title'] = $this->title;
		$data['l_meta_name'] = $this->meta_name;
		$data['l_meta_http_equiv'] = $this->meta_http_equiv;
		$data['l_javascript_variables'] = $this->javascript_variables;

		if (empty($this->layout)) {
			$this->load->view($template, array_merge($data, $this->global_data));
		} else {
			$data['l_template'] = $template;
			$this->load->view($this->layout, array_merge($data, $this->global_data));
		}
	}

	protected function json_back($status, $msg = '', $data = array()) {
		header('Content-type: application/json; charset=utf-8');
		echo json_encode(array(
			'status' => $status,
			'msg' => $msg,
			'data' => $data
		));
		exit(0);
	}

	protected function upload_back($status, $msg = '', $data = array(),$callback = 'Z.uploadBack') {
		$json_data = json_encode($data);
		echo "<script type=\"text/javascript\">\n
                    this.parent.window.{$callback}('{$status}','{$msg}',{$json_data});\n
              </script>\n";
		exit(0);
	}

	public function _output($output) {
		//替换模版中的静态文件路径
		$output = preg_replace("/((:?src|href)=\")\/assets/", '$1' . SITE_URL . '/assets', $output);
		$output = preg_replace("/((:?src|href|action)=\")\/(?!(:?assets|http))/", '$1' . SITE_URL . '/index.php/', $output);

		echo $output;
	}

}